using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using OpenComposite.EII.Repository;
using OpenComposite.Repository.Properties;

namespace OpenComposite.EII.RepositoryUIs
{
	public partial class UCCapability : RepositoryItemUI
	{
		#region Initialization

		public UCCapability(IServiceProvider serviceProvider)
			: base(serviceProvider)
		{
			InitializeComponent();
		}

		#endregion

		#region Public Members

		#region Properties
		public override IRepositoryItem RepositoryItem
		{
			get
			{
				save();
				return _capability;
			}
			set
			{
				base.RepositoryItem = value;
				if ( value is Capability ) {
					_capability = (Capability)value;
					load();
				}
			}
		}

		public override string TypeText
		{
			get { return _capability.RepositoryItemTypeName; }
		}

		public override string TypeDescription
		{
			get { return ""; }
		}
		#endregion

		#region Events
		#endregion

		#region Methods
		#endregion

		#endregion

		#region Private Members

		#region Variables
		private Capability _capability = null;
		#endregion

		#region Methods
		private void load()
		{
			if ( _capability == null )
				return;

			txtName.Text = _capability.Name;
			txtDesc.Text = _capability.Description;
		}
		private void save()
		{
			if ( _capability == null ) throw new NullReferenceException();

			_capability.Reload();

			_capability.Name = txtName.Text;
			_capability.Description = txtDesc.Text;
		}
		#endregion

		private void txtName_KeyPress(object sender, KeyPressEventArgs e)
		{
			if ( e.KeyChar < (char)32 ) return;
			e.Handled = !Helpers.IsValidCharForName(e.KeyChar);
		}

		#region Event Handlers
		#endregion

		#endregion

		protected override void NotifyPropertyChange(string property, object value)
		{
			if ( property == "Name" && value != null ) {
				txtName.Text = value.ToString();
			}
		}
	}
}
